package com.diy.sigmund.diycommon.designpatterns.template;

import java.util.Arrays;
import java.util.StringJoiner;

/**
 * 鸭子数组排序
 *
 * @author ylm-sigmund
 * @since 2022/2/10 14:39
 */
public class Duck implements Comparable {

    String name;
    /** 体重 */
    int weight;

    public Duck(String name, int weight) {
        this.name = name;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Duck.class.getSimpleName() + "[", "]")
                .add("name='" + name + "'")
                .add("weight=" + weight)
                .toString();
    }

    @Override
    public int compareTo(Object o) {
        Duck other = (Duck) o;
        // return Integer.compare(this.weight, other.weight);
        // 以下为升序
        if (this.weight < other.weight) {
            return -1;
        } else if (this.weight == other.weight) {
            return 0;
        } else {
            return 1;
        }
    }

    public static void main(String[] args) {
        Duck[] ducks = new Duck[]{
                new Duck("tom1", 2),
                new Duck("tom2", 4),
                new Duck("tom3", 1),
                new Duck("tom4", 7),
                new Duck("tom5", 5),
                new Duck("tom6", 3),
                new Duck("tom7", 2),
        };

        System.out.println(Arrays.toString(ducks));

        Arrays.sort(ducks);

        System.out.println("=============");
        System.out.println(Arrays.toString(ducks));
    }
}
